<?php
class Library_Model_NguoiDungMapper extends Library_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator = null;
	
	public function searchById($id){
		$db = $this->getDb();
		$select = $db->select()
							->from('tbl_nguoidung')->where('MaND = ?', $id);
		$nguoidung = $db->fetchRow($select);
		
		return $this->_populate($nguoidung);
	}
	public function nguoidungList(){
		$db = $this->getDb();
		$selectNguoiDung = $db->select()
											->from('tbl_nguoidung');
		$nguoidungs = $db->fetchAll($selectNguoiDung);
		$nguoidungObjArray = array();
		foreach($nguoidungs as $nguoidung){
			$nguoidungObjArray[] = $this->_populate($nguoidung);
		}
		return $nguoidungObjArray;
	}
	
	public function getAllUsers()
	{
		$db = $this->getDb();
		$selectUsers = $db->select()
		->from('tbl_nguoidung');
		$nguoidungs = $db->fetchAll($selectUsers);
	
		$nguoidungsObjArray = array();
	
		foreach($nguoidungs as $nguoidung)
		{
			$nhanvienMapper = Library_Model_NhanVienMapper::getInstance();
			$nhanvien = $nhanvienMapper->searchNhanVienById($nguoidung['MaND']);
			$nguoidung['customers'] = $nhanvien;
	
			$nguoidungsObjArray[] = $this->_populate($nguoidung);
		}
		return $nguoidungsObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_NguoiDung();
		$obj->setId($data['MaND']);
		$obj->setTennd($data['TenNguoiDung']);
		$obj->setEmail($data['Email']);
		$obj->setTendangnhap($data['TenDangNhap']);
		$obj->setMatkhau($data['MatKhau']);
		$obj->setKhoa($data['Khoa']);
		$obj->setQuyenhan($data['QuyenHan']);
		
		return $obj;
	}
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data = array(
				'TenNguoiDung' 	=> 	$obj->getTennd(),
				'Email'					=>	$obj->getEmail(),
				'TenDangNhap'	=>	$obj->getTendangnhap(),
				'MatKhau'			=>	$obj->getMatkhau(),
				'Khoa'					=>	$obj->getKhoa(),		
				'QuyenHan'			=>	$obj->getQuyenhan()
				);
		$db->insert('tbl_nguoidung', $data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data = array(
				'TenNguoiDung' 	=> 	$obj->getTennd(),
				'Email'					=>	$obj->getEmail(),
				'TenDangNhap'	=>	$obj->getTendangnhap(),
				'MatKhau'			=>	$obj->getMatkhau(),
				'Khoa'					=>	$obj->getKhoa(),
				'QuyenHan'			=>	$obj->getQuyenhan()
		);
		$where = $db->quoteInto('MaND = ?', $obj->getId());
		$db->update('tbl_nguoidung', $data, $where);
	}
	public function delete($id){
		$db = $this->getDb();
		$where = $db->quoteInto('MaND = ?', $id);
		$db->delete('tbl_nguoidung', $where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	/* public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	} */
	
}